//
//  DepartmentView.m
//  MyDemo2
//
//  Created by ZSMAC on 2018/10/8.
//  Copyright © 2018年 张帅. All rights reserved.
//

#import "DepartmentView.h"
#import "GoodsCell.h"
#import "ZRootTabBarViewController.h"
#import "loginViewController.h"
#import "GoodsModel.h"
#import "GoodsDetailsController.h"

@implementation DepartmentView

- (instancetype)initWithFrame:(CGRect)frame {
    self=[super initWithFrame:frame];
    if (self) {
        _dataArray=[[NSMutableArray alloc] init];
        [self uiConfigure];
    }
    return self;
}

/**
 刷新方法

 @param externalRefresh 是否是外部刷新
 */
- (void)refreshDataExternalRefresh:(BOOL)externalRefresh {
    if (externalRefresh) {
        if (_dataArray.count==0) {
            NSLog(@"当前显示 %@",self.goodsType);
            [self.collectionView.mj_header beginRefreshing];
        } 
    }
}
- (void)getData:(BOOL)refresh {
    BmobQuery   *bquery = [BmobQuery queryWithClassName:Goods_list_Name];
    bquery.limit = 4;//一次多少条
    [bquery selectKeys:@[@"goodsType",@"title",@"UnitPrice",@"coverImageUrl",@"objectId"]];//指定字段
    [bquery whereKey:@"goodsType" equalTo:self.goodsType];
    [bquery orderByDescending:@"updatedAt"];//根基创建时间降序查询 时间最近的
    if (!refresh) {
        bquery.skip=self.dataArray.count;//跳过几条数据
    }
    //查找GameScore表的数据
    @WeakObj(self);
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (refresh) {
            [selfWeak.collectionView.mj_header endRefreshing];
            [selfWeak.collectionView.mj_footer endRefreshing];
            [selfWeak.dataArray removeAllObjects];
        } else {
            if (array.count==0) {
                [selfWeak.collectionView.mj_footer endRefreshingWithNoMoreData];//提示没有没有更多数据了
            } else {
                [selfWeak.collectionView.mj_footer endRefreshing];
            }
        }
        for (BmobObject *obj in array) {
            NSDictionary * dic = [obj valueForKey:@"dataDic"];
            GoodsModel *model = [GoodsModel mj_objectWithKeyValues:dic];
            [selfWeak.dataArray addObject:model];
        }
        [selfWeak.collectionView reloadData];
    }];
}

- (void)uiConfigure {
    UICollectionViewFlowLayout *flatout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat width = (SCREEN_WIDTH-30-5)/2;
    CGFloat height = (SCREEN_HEIGHT-170)/2;
    flatout.itemSize = CGSizeMake(width, height);
    flatout.minimumLineSpacing = 5;
    flatout.minimumInteritemSpacing = 5;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(15, 0, self.width-30, self.height) collectionViewLayout:flatout];
    _collectionView.dataSource=self;
    _collectionView.delegate=self;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.backgroundColor=[UIColor clearColor];
    [self addSubview:_collectionView];
    [_collectionView registerClass:[GoodsCell class] forCellWithReuseIdentifier:@"GoodsCell"];
    @WeakObj(self);
    // 设置颜色
    _collectionView.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [selfWeak getData:YES];
    }];
    _collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [selfWeak getData:NO];
    }];
    [_collectionView addPromptViewType:Not_Data_Type];
}
- (void)delatLoad {
    [self.collectionView.mj_header endRefreshing];
}

#pragma mark UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.dataArray.count==0) {
        [collectionView promptViewIsHidden:NO];
    } else {
        [collectionView promptViewIsHidden:YES];
    }
    return self.dataArray.count;
}
#pragma mark UICollectionViewCell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GoodsCell * cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"GoodsCell" forIndexPath:indexPath];
    GoodsModel *model =self.dataArray[indexPath.row];
    cell.model=model;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    GoodsCell * cell = (GoodsCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [UIView animateWithDuration:0.2 animations:^{
        cell.transform = CGAffineTransformMakeScale(0.9, 0.9);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.2 animations:^{
         cell.transform = CGAffineTransformMakeScale(1, 1);
        } completion:^(BOOL finished) {
            [self puchGoodsDetailsController:indexPath];
        }];
    }];
}
- (void)puchGoodsDetailsController:(NSIndexPath *)indexPath {
    GoodsDetailsController *DetailsVC = [[GoodsDetailsController alloc] init];
    GoodsModel *model = self.dataArray[indexPath.row];
    DetailsVC.goodsId=model.objectId;
    [[MPublicManager sharedPublicManager].currentControllr.navigationController pushViewController:DetailsVC animated:YES];
}
@end
